﻿'***************************************************************************\
' Module Name: SystemWindowsAutomationExtensions.vb
' Project: SetDefaultAudioEndpoint http://sdae.codeplex.com/
' Copyright 2011 by jeff
' 
' This source is subject to the GNU General Public License version 2 (GPLv2).
' See http://www.gnu.org/licenses/gpl-2.0.html.
' All other rights reserved.
' 
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
'***************************************************************************/

Imports System.Windows.Automation

Module SystemWindowsAutomationExtensions

  <System.Runtime.CompilerServices.Extension()> _
  Public Function FindFirstInChildrenByName(ByVal element As AutomationElement, ByVal name As String) As AutomationElement
    Return element.FindFirst(TreeScope.Children, New PropertyCondition(AutomationElement.NameProperty, name, PropertyConditionFlags.IgnoreCase))
  End Function

  <System.Runtime.CompilerServices.Extension()> _
  Public Function FindFirstInChildrenByAutomationId(ByVal element As AutomationElement, ByVal automationId As String) As AutomationElement
    Return element.FindFirst(TreeScope.Children, New PropertyCondition(AutomationElement.AutomationIdProperty, automationId))
  End Function

  <System.Runtime.CompilerServices.Extension()> _
  Public Sub SelectElement(ByVal element As AutomationElement)
    Dim target As SelectionItemPattern = DirectCast(element.GetCurrentPattern(SelectionItemPattern.Pattern), SelectionItemPattern)

    target.Select()
  End Sub

  <System.Runtime.CompilerServices.Extension()> _
  Public Sub ClickElement(ByVal element As AutomationElement)
    Dim target As InvokePattern = DirectCast(element.GetCurrentPattern(InvokePattern.Pattern), InvokePattern)

    target.Invoke()
  End Sub

End Module
